package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.DTO.CommitDTO;
import com.woniuxy.entity.DTO.GoodsType;
import com.woniuxy.entity.Goods;
import com.woniuxy.entity.GoodsCommit;
import com.woniuxy.entity.GoodsStoreCondition;
import com.woniuxy.entity.GoodsUnit;
import com.woniuxy.service.GoodsService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.util.MinioUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.woniuxy.util.ResponseEnum.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsService goodsService;
    @Resource
    private MinioUtil minioUtil;

    @PostMapping("/queryGoods")
    public ResponseData queryGoods(@RequestBody PageParam<Goods> pageParam) { //分页查询, 接受泛型<>
        PageInfo pageInfo = goodsService.queryGoodsServer(pageParam);
        return ResponseData.success(pageInfo);
    }

    @PostMapping("/queryGoodsApp")
    public ResponseData queryGoodsApp(@RequestBody Goods goods) { //App端口查询, 不用泛型
        List<Goods> goodsList = goodsService.queryGoodsApp(goods);
        return ResponseData.success(goodsList);
    }

    @PostMapping("/addGoods")
    public ResponseData addGoods(@RequestBody Goods goods) { //添加商品
        goodsService.addGoods(goods);
        return ResponseData.success(null);
    }

    @PostMapping("/queryGoodsType")
    public ResponseData queryGoodsType(@RequestBody PageParam<GoodsType> pageParam) { //查询所有商品分类
        PageInfo pageInfo = goodsService.queryGoodsType(pageParam);
        return ResponseData.success(pageInfo);
    }

    @GetMapping("/queryTypeToDropdownBox")
    private ResponseData queryTypeToDropdownBox() { //查询商品分类作为下拉框数据回显
        List<GoodsType> goodsTypeList = goodsService.queryTypeToDropdownBox();
        return ResponseData.success(goodsTypeList);
    }

    @GetMapping("/queryStoreConditionToDropdownBox")
    private ResponseData queryStoreConditionToDropdownBox() { //查询商品存储条件作为下拉框数据回显
        List<GoodsStoreCondition> goodsStoreConditionList = goodsService.queryStoreConditionToDropdownBox();
        return ResponseData.success(goodsStoreConditionList);
    }

    @GetMapping("/queryUnitToDropdownBox")
    private ResponseData queryUnitToDropdownBox() { //查询商品计量单位作为下拉框数据回显
        List<GoodsUnit> goodsUnitList = goodsService.queryUnitToDropdownBox();
        return ResponseData.success(goodsUnitList);
    }

    @GetMapping("/changeGoodsStatus/{id}")
    private ResponseData changeGoodsStatus(@PathVariable("id") Integer id) { //改变商品上架下架状态值
        boolean isChange = goodsService.changeGoodsStatus(id);
        if (isChange) {
            return ResponseData.success(null); //修改成功
        } else {
            return ResponseData.fail(CHANGE_FAIL); //修改状态值失败, 因为要修改的商品正在被其他线程修改
        }
    }

    @PostMapping("/uploadImg")  //上传图片到minio
    public ResponseData uploadImg(MultipartFile file, HttpServletRequest request) throws Exception {
        try {
            String imgPath = minioUtil.uploadFile(file, "image");
            return ResponseData.success(imgPath);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @GetMapping("/queryGoodsById/{id}")
    private ResponseData queryGoodsById(@PathVariable("id") Integer id) { //按照商品表id查找对应商品
        Goods goods = goodsService.queryGoodsById(id);
        return ResponseData.success(goods);
    }

    @PostMapping("/updateGoods")
    public ResponseData updateGoods(@RequestBody Goods goods) { //更新商品信息
        boolean isUpdate = goodsService.updateGoods(goods);
        if (isUpdate) {
            return ResponseData.success(null); //更新成功
        } else {
            return ResponseData.fail(UPDATE_FAIL); //更新失败, 因为要修改的商品正在被其他线程修改
        }
    }

    @PostMapping("/addGoodsType")
    public ResponseData addGoodsType(@RequestBody GoodsType goodsType) { //增加商品分类(一二级分类共用)
        goodsService.addGoodsType(goodsType);
        return ResponseData.success(null);
    }

    @PostMapping("/updateGoodsType")
    public ResponseData updateGoodsType(@RequestBody GoodsType goodsType) { //更新商品分类信息(一二级分类共用)
        boolean isUpdate = goodsService.updateGoodsType(goodsType);
        if (isUpdate) {
            return ResponseData.success(null); //更新成功
        } else {
            return ResponseData.fail(UPDATE_FAIL); //更新失败, 因为要修改的分类正在被其他线程修改
        }
    }

    @GetMapping("/queryGoodsTypeByFirst")
    private ResponseData queryGoodsTypeByFirst() { //分类信息界面的下拉框回显
        List<GoodsType> goodsTypeList = goodsService.queryGoodsTypeByFirst();
        return ResponseData.success(goodsTypeList);
    }

    @GetMapping("/queryGoodsTypeById/{id}/{level}")
    private ResponseData queryGoodsTypeById(@PathVariable("id") Integer id,
                                            @PathVariable("level") String level) { //分类信息编辑回显
        GoodsType goodsType = goodsService.queryGoodsTypeById(id, level);
        return ResponseData.success(goodsType);
    }

    @PostMapping("/updateGoodsTypeStatus")
    public ResponseData updateGoodsTypeStatus(@RequestBody GoodsType goodsType) { //修改商品分类的启用停用状态(一二级分类共用)
        int tryChange = goodsService.updateGoodsTypeStatus(goodsType);
        if (tryChange == 200) {
            return ResponseData.success(null); //修改成功
        } else if (tryChange == 4031) {
            return ResponseData.fail(ERROR_PARAM_FIRST_TYPE, null); //拒绝修改分类, 因为牵连到下级分类表外键
        } else if (tryChange == 4032) {
            return ResponseData.fail(ERROR_PARAM_SECOND_TYPE, null); //拒绝修改分类, 因为牵连到商品表外键
        } else {
            return ResponseData.fail(CANCEL_FAIL); //修改状态值失败, 因为要修改的分类正在被其他线程修改
        }
    }

    @GetMapping("/queryGoodsCommit/{id}")
    private ResponseData queryGoodsCommit(@PathVariable("id") Integer id) { //分类信息编辑回显
        List<CommitDTO> commitDTOList = goodsService.queryGoodsCommitByGoodsId(id);
        return ResponseData.success(commitDTOList);
    }

}
